<%@page import="java.util.StringTokenizer"%>
<%@page import="java.io.IOException"%>
<%@page import="java.text.ParseException"%>
<%@page import="java.sql.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.sql.Connection"%>
<%@page import="by.epam.communalservices.entity.JkhObject"%>
<%@page import="by.epam.communalservices.dao.jdbc.JdbcDaoJkhObject"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib tagdir="/WEB-INF/tags/connect" prefix="connect"%>
<%@ taglib tagdir="/WEB-INF/tags/table" prefix="table"%>
<%@ taglib tagdir="/WEB-INF/tags/object" prefix="object"%>
<%@ taglib tagdir="/WEB-INF/tags/user" prefix="user"%>
<%@ taglib tagdir="/WEB-INF/tags/task" prefix="task"%>

<connect:isset/>

<connect:denny rules="admin,edit,object_edit,object_view,objecter,tasker,brigadir" />

<connect:rule val="editor" rules="admin,edit,object_edit" />
<connect:rule val="objecter" rules="tasker" />
<connect:rule val="objecter" rules="objecter" />

<object:edit idOwner="${param.idOwner}" address="${param.address}" id="${param.id}"
	name="${param.name}" surname="${param.surname}"
	patronymic="${param.patronymic}"
	date_last_repair="${param.date_last_repair}" submit="${param.submit}"
	delete="${param.delete}" />

<c:if test="${requestScope.message ne ''}">
	<p class='message'>
		<c:out value="${message}" />
	</p>
</c:if>
<c:if test="${param.delete gt 0}">
	<c:import url="/WEB-INF/pages/object_view.jsp"  />
</c:if>
<c:if test="${form}">
	<form name='object' method='POST' action='object_edit'>
		<table:body style='width:50%'>
			<table:title span="4">
				<c:choose>
					<c:when test="${param.id gt 0}">
      				Редактирование объекта ЖКХ
  				</c:when>
					<c:otherwise>
      				Добавление объекта ЖКХ
  				</c:otherwise>
				</c:choose>
			</table:title>

			<input name='id' value="${requestScope.object.id}" type="hidden">
			<tr id=c>
				<td colspan=2 class=t>Address</td>
				<td colspan=2 class=t id=l>
					<c:choose>
					<c:when test="${editor or objecter}">
						<input name='address' value="${requestScope.object.address}" />
					</c:when>
					<c:otherwise>
						<c:out value="${requestScope.object.address}" />
					</c:otherwise>
					</c:choose>	
				</td>
			</tr>

			<tr id=n>
				<td colspan=2 class=t>Surname</td>
				<td colspan=2 class=t id=l>
					<c:choose>
					<c:when test="${editor or objecter}">
						<input name='surname' value="${requestScope.object.surname}" />
					</c:when>
					<c:otherwise>
						<c:out value="${requestScope.object.surname}" />
					</c:otherwise>
					</c:choose>	
				</td>
			</tr>

			<tr id=c>
				<td colspan=2 class=t>Name</td>
				<td colspan=2 class=t id=l>
					<c:choose>
					<c:when test="${editor or objecter}">
						<input name='name' value="${requestScope.object.name}" />
					</c:when>
					<c:otherwise>
						<c:out value="${requestScope.object.name}" />
					</c:otherwise>
					</c:choose>	
				</td>
			</tr>

			<tr id=n>
				<td colspan=2 class=t>Patronymic</td>
				<td colspan=2 class=t id=l>
					<c:choose>
					<c:when test="${editor or objecter}">
						<input name='patronymic' value="${requestScope.object.patronymic}" />
					</c:when>
					<c:otherwise>
						<c:out value="${requestScope.object.patronymic}" />
					</c:otherwise>
					</c:choose>	
				</td>
			</tr>

			<tr id=c>
				<td colspan=2 class=t>Date last</td>
				<td colspan=2 class=t id=l>
					<c:choose>
					<c:when test="${editor or tasker}">
						<table:calendar key="d0" form="object" input="date_last_repair" />
						<input name='date_last_repair'	value="${requestScope.object.dateLastRepair}" />
  					</c:when>
					<c:otherwise>
						<c:out value="${requestScope.object.dateLastRepair}" />
					</c:otherwise>
					</c:choose>	
				</td>
			</tr>	
			<tr id=n>
				<td colspan=2 class=t>Owner</td>
				<td colspan=2 class=t id=l>
					<c:choose>
					<c:when test="${editor}">
						<user:select name="idOwner" attr="style='width:140px;'"
						selected="${requestScope.object.idOwner}" roleMarker="objecter" />
  					</c:when>
					<c:otherwise>
						<user:name id="${requestScope.object.idOwner}" />
					</c:otherwise>
					</c:choose>	
				</td>
			</tr>
			
			<table:title span="4" down="yes">
				<input name='submit' value='save' type='submit'>
			</table:title>
		</table:body>
	</form>
	
	<c:if test="${form}">
		<br/><br/>
		<object:task_list id="${requestScope.object.id}" />
		<table:body>
			<table:title span="8">
		   		Task List!!!
			</table:title>
			<table:head names="id,idObject,idTeam,jobDescription,performTime,status,request,action" />
			<c:set var="index" value="0" />
			<c:forEach items="${requestScope.taskList}" var="task">
				<c:set var="index" value="${index + 1}" />
				<task:line  task="${task}" dif="${index mod 2 eq 0 ? 'c' : 'n'}" />	
			</c:forEach>

			<table:title span="8" down="yes">
			<c:if test="${editor or objecter}">
				<button onclick="document.location.href='task_edit?idObject=${requestScope.object.id}'"
					style='float: right;'>Add New task</button>
			</c:if>
			</table:title>
		</table:body>
	</c:if>
</c:if>
